package cn.hiapi.core.utils;

import cn.hiapi.core.basic.Nuwa;
import com.alibaba.fastjson2.JSONObject;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author AdinZ
 * @date 2024/1/12 10:24
 */
public class JsonUtils extends Nuwa {

    public static BigDecimal getBigDecimal(JSONObject json, String key, BigDecimal def) {
        if (json == null) return def;
        final BigDecimal decimal = json.getBigDecimal(key);
        if (decimal != null) return decimal;
        return def;
    }

    public static BigDecimal getBigDecimal(JSONObject json, String key) {
        return getBigDecimal(json, key, BigDecimal.ZERO);
    }

    public static int getInt(JSONObject data, String key, int def) {
        if (data == null) return def;
        Integer integer = data.getInteger(key);
        if (integer == null) return def;
        return integer;
    }
    public static long getLong(JSONObject config, String key, long def) {
        if (config == null || config.isEmpty() || !config.containsKey(key)) return def;
        Long l = config.getLong(key);
        if (l == null) return def;
        return l;
    }

    public static long getLong(JSONObject config, String key) {
        return getLong(config, key, 0);
    }

    public static BigInteger getBigInteger(JSONObject config, String key, BigInteger def) {
        if (config == null || config.isEmpty() || !config.containsKey(key)) return def;
        BigInteger l = config.getBigInteger(key);
        if (l == null) return def;
        return l;
    }

    public static BigInteger getBigInteger(JSONObject config, String key) {
        return getBigInteger(config, key, BigInteger.ZERO);
    }

    public static class Builder {
        private final JSONObject json = new JSONObject();

        public Builder put(String key, Object value) {
            json.put(key, value);
            return this;
        }
        public JSONObject build() {
            return json;
        }
    }

    public static Builder builder(){return new Builder();}



}
